/*
 * pos.c
 *
 *  Created on: May 27, 2025
 *      Author: lsy18
 */
#include "../Communicate/Uart_slave/uart_slave.h"
#include "wit_c_sdk/wit_c_sdk.h"
#include "../Communicate/Uart_com/uart_it.h"

#include "../Communicate/Uart_slave/Uart_slave_REG/uart_slave_reg.h"


#include "pos.h"

#include "../Hardware/Led/led.h"



DMA_STURCT pos_dma;

uint16_t pos_jy_init_data;

void pos_write(uint8_t *p_ucData, uint32_t uiLen)
{
	HAL_UART_Transmit(&huart_pos, p_ucData, uiLen, 1000);
}


void pos_init()
{
	WitDeInit();
	WitInit(WIT_PROTOCOL_NORMAL, 000);
	WitSerialWriteRegister(pos_write);
	WitSetUartBaud(huart_pos.Init.BaudRate);
	pos_dma.buff_num = wit_receivw_size;
	pos_uart.buff_num = wit_receivw_size;
	//hdma_pos.XferCpltCallback = HAL_DMA_XferCpltCallback;
	//HAL_DMA_RegisterCallback(&hdma_pos, HAL_DMA_XFER_CPLT_CB_ID, HAL_DMA_XferCpltCallback);
	HAL_UART_Receive_IT(&huart_pos, &pos_uart.uart_bit_buff, 1);
	//HAL_Delay(100);
	//pos_jy_init_data = ( uint16_t )( reg_array[YAW_H_REG] << 8 | reg_array[YAW_L_REG] );
}

uint8_t pos_data_exam()
{
	uint8_t sum = 0;
	for(int i=0;i < pos_uart.buff_num -1 ;i++)
	{
		sum = sum + pos_uart.uart_buff[i];
	}
	if((pos_uart.uart_buff[pos_uart.buff_num-1] == sum) &&(pos_uart.uart_buff[0] == wit_star))
		return 1;
	else
		return 0;
}

void pos_exam()
{
	if(pos_uart.receive_num == pos_uart.buff_num)
	{
		if(pos_data_exam())
		{
			pos_uart.receive_num = 0;
			switch(pos_uart.uart_buff[1])
			{
			case 0x51:
				break;
			case 0x52:
				break;
			case 0x53:
//				uint16_t pos_data;
//				pos_data = ( uint16_t )( pos_uart.uart_buff[7] << 8 | pos_uart.uart_buff[6] );
//				pos_data = (pos_data > pos_jy_init_data) ? (pos_data - pos_jy_init_data) : (pos_data + (65535 - pos_jy_init_data));
//				pos_uart.uart_buff[7] = (pos_data>>8)&0xFF;
//				pos_uart.uart_buff[6] = pos_data & 0xFF;
				//led_turn();
				uart_slave_reg_write(&pos_uart.uart_buff[2], ROLL_L__REG, 0x06);
				break;
			}
		}
		else
		{
			uart_data_translocate(pos_uart.uart_buff , 0, pos_uart.buff_num);
			pos_uart.receive_num = pos_uart.receive_num - 1;
		}
	}
	HAL_UART_Receive_IT(&huart_pos, &pos_uart.uart_bit_buff, 1);
}

